

$(document).ready(function () {
    init( callback, 1);
    //图片
    initDropzone();
});


function init( callback, current) {
    $("#thislist").html("");
    $.ajax({
        type: "get",
        url: urlcore + "/api/mygPainting/selectPageAdmin?current=" + current ,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                callback(data.data.records);
                $('#thiscount').text(data.data.total);
                $("#pager").pager({
                    pagenumber: current,
                    pagecount: data.data.pages,
                    totalcount: data.data.total,
                    buttonClickCallback: PageClick
                });
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("失败");
        }
    });

}

//回调函数
PageClick = function (pageclickednumber) {
    init( callback,pageclickednumber);
}

var callback = function (data) {
    var html = "", goodsName = "",  goodsUrl = "",
         gmtDatetime = "", id = "";
    console.log(data)
    var picUrl = "";
    for (var i = 0; i < data.length; i++) {
        id = data[i].id;
        goodsName = data[i].goods.goodsName;
        goodsUrl = data[i].goods.goodsUrl != null ? data[i].goods.goodsUrl : "";
        var price = data[i].goods.mPrice != null ? data[i].goods.mPrice : "";
        picUrl = data[i].picUrl != null ? data[i].picUrl : "";
        gmtDatetime = data[i].gmtDatetime != null ? new Date(data[i].gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss") : "";
        html += "  <tr id=\"" + id + "\">\n" +
            "  \t\t<td>" + (i + 1) + "</td>\n" +
            "  \t\t<td>" + goodsName + "</td>\n" +
            "  \t\t<td>" + price + "</td>\n" +
            "\t\t <td class=\"footable-visible\"><img width=\"auto\" height=\"50px\" src=\"" + picUrl + "\"></td>\n" +
            "  \t\t<th>" + gmtDatetime + "</th>\n" +
            "  \t\t<td>\n" +
            "   \t\t\t<a class=\"btn btn-info btn-xs\" data-toggle=\"modal\" data-target=\"#myModal\" onclick=\"editShow("+id+")\" href=\"javascript:;\"> 修改</a>\n" +
            "   \t\t\t<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"doDelete(" + id + ")\"> 删除</a>\n" +
            "  \t\t</td>\n" +
            "  \t</tr>";
    }
    $("#thislist").append(html);
}





//刪除
function doDelete(id) {

    $.ajax({
        type: "GET",
        url: urlcore + "/api/mygPainting/deleteMygPainting?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                location.reload();
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("删除失败");
        }
    });
}


function initDropzone() {
    Dropzone.autoDiscover = false;//解决两次实例Dropzone错误，可在控制台看到该错误
// Dropzone class:
    myDropzone = new Dropzone("div#editPic",
        {
            url: urlcore + "/api/attachment/upload",
//                                        paramName: "skuUrl", // The name that will be used to transfer the file
            maxFiles: 1,
            thumbnailWidth: 100,
            thumbnailHeight: 100,
            addRemoveLinks: true,//清除图片
            dictRemoveFile: "删除图片",//如果addRemoveLinks为 true，这段文本用来设置删除文件显示文本.
            dictCancelUpload: "取消上传",//如果addRemoveLinks为 true，这段文本用来设置取消上载链接的文本
            previewsContainer: "#editPic-pre", // 文件预览显示位置,可以为id或class选择器或html元素
            success: function (file, data) {
                //这里的$(this)其实得到的是Dropzone对象（对象数组，取第一个为当前Dropzone对象）
                console.log($(this));
                //通过查看Dropzone对象的结构得到对应div的id
                var sku_url_id = '#' + $(this)[0].element.id;
               /* console.log("file:", file);
                console.log("data:", data);*/
                var curPicUrl = data.data[0];
                // curPicUrl = skuUrl;
                //放进数组中
                editPicUrlArr.push(curPicUrl);
                //详情多张轮播图url拼接
                // curDetailGoodsUrl += "," + curPicUrl;
                console.log("sku-url:", curPicUrl);
                //为div中的data-url属性赋值
                $(sku_url_id).attr("data-url", curPicUrl);
                //隐藏上传图片按钮
                $(sku_url_id).find('a[class*="upload_sku_pic"]').hide();
            },
            init: function () {
                this.on("addedfile", function (file) {
                    console.log("addedfile");// actions...

                });
                this.on("removedfile", function (file) {
                    //通过$(this)对象（Dropzone对象）得到当前div的id
                    var cur_skudiv_id = '#' + $(this)[0].element.id;
                    /*移除图片时显示上传图片按钮*/
                    $(cur_skudiv_id).find('a[class*="upload_sku_pic"]').show();
                    console.log("removedfile", file);// actions...
                });
            }
        });

}



var editPicUrlArr = [];
var curPicUrl;
var myDropzone;
var curEditId = "";





function  editShow(id) {
    var p = $("#"+id).find("td").eq(2).html();
    console.log( $("#"+id).find("td").eq(2).html());
    $("#prices").val(p);
    var n = $("#"+id).find("th").eq(0).html();
    $("#number").val(n);

    var pic = $("#"+id).find("td").eq(3).children("img").attr("src");
    $("#pic").attr("src",pic);
    curEditId = id;

}




function saveEditPic() {
    if (editPicUrlArr.length == 0) {
        //alert("请先选择图片");
        //return;
    }else if(editPicUrlArr.length > 1){
        alert("只能上传一张图片");
        return;
    }


    curPicUrl = editPicUrlArr[0];
    $.ajax({
        url: urlcore + "/api/mygPainting/update",
        type: "post",
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            id: curEditId,
            picUrl: curPicUrl
        }),
        success: function (cbdata) {
            curPicUrl = "";
            alert("修改成功");
            location.reload();
        },
        error: function (cbdata) {
            alert("更新状态失败");
        }
    });
}